/*
md380.h
offer md380 series motor controller driver ,function and realise
designed by lunanting

--------------------------------
2020-10-26 17:50:46
create file
--------------------------------
*/

#include "md380.h"
#include "../../bsp/modbus_485.h"
#include "../../bsp/cli_cmd.h"

static MD380_data md380={0};


/*
md380 init 
param:
    port:uart port
    speed:uart speed
return:
    0=success
*/
int xp_md380_init(int port,int speed,int id){
    int sta=0;

    memset(&md380,0,sizeof(md380));
    md380.port=port;
    md380.speed=speed;
    md380.id=id;
    sta=xp_modbus_init(port,speed,MB_PAR_NONE,100);
    if(sta!=0){
        println("md380 init fail~,modbus init fail~");
        md380.init=0;
        return -1;
    }

    xp_md380_freq_set(100);
    xp_md380_cmd_set(MD380_free_stop);
    xp_md380_cmd_set(MD380_err_reset);
    println("md380 init success~~");
    md380.init=1;
    return 0;
}

int xp_md380_read(u16 addr){
    int sta=0;
    MB_data modbus;
    u8 count=5;
    u8 rec_buf[5]={0};
    u8 rec_len=0;

    modbus.slave_id=md380.id;
    modbus.mb_fun=mb_single_r;
    modbus.start_addr=addr;
    modbus.rwLen=2;
    modbus.resBuf=rec_buf;
    modbus.resLen=&rec_len;
    while(count--){
        sta=xp_modbus_ask(md380.port,&modbus);
        if(sta==0){
            if(0 == modbus.resBuf){
                return 0;
            }else{
                return -1;
                println("md380 %d,error code: %d.%d",md380.id,rec_buf[0],rec_buf[1]);
            }
        }
    }
    if(sta!=0){
        println("md380 read failed");
        return -2;
    }
}

/*
md380 write
param:
    data:write data
    len:data lenght
return:
    0=success
*/
int xp_md380_write(u16 addr,char *data){
    MB_data modbus={0};
    int sta=0;
    u8 rec_data[10];
    u8 rec_len;
    u8 count=5;

    if(md380.init==0){
        println("md380 motor controller no init~");
        return -1;
    }
    modbus.slave_id=md380.id;
    modbus.mb_fun=mb_single_w;
    modbus.start_addr=addr;
    modbus.wBuf=&data;
    modbus.rwLen=1;
    modbus.resBuf=rec_data;
    modbus.resLen=&rec_len;
    while(count--){
        if(xp_modbus_ask(md380.port,&modbus)==0)return 0;
    }
    println("md380 write fail~");
    return -1;    
}



/*
set freq
设定频率百分比
param:
    freq:freq value
return:
    0=success
*/
int xp_md380_freq_set(int freq){
    int f;

    md380.freq=freq;
    f=freq*100;
    return xp_md380_write(0x1000,f);
}

/*
run cmd set
运行指令设定
param:
    cmd:run cmd
return:
    0=success
*/
int xp_md380_cmd_set(MD380_cmd cmd){
    md380.cmd=cmd;
    return xp_md380_write(0x2000,cmd);
}




/********************************************debug*********************************/
/*
md380 debug
*/
int xp_md380_debug(char *type,char *fun,char *param){
    int i;
    MD380_cmd cmd;

    if(strcmp(type,"md380")!=0)return 0;
    else if(strcmp(fun,"set_freq")==0){
        i=atoi(param);
        if(-101<i&&i<101){
            println("md380 freq set as： %d",i);
            xp_md380_freq_set(atoi(param));
        }
        else{
            println("md380 freq set fail~ freq=%d",i);
        }
    }
    else if(strcmp(fun,"set_cmd")==0){
        //=1为开，=5为关
        cmd=(MD380_cmd)atoi(param);
        xp_md380_cmd_set(cmd);
    }
    else{
        return 0;
    }
    return 1;
}




//end of file
